package com.example.mq.mqserver.datacenter;

import com.example.mq.MqApplication;
import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.ExchangeType;
import com.example.mq.mqserver.core.MSGQueue;
import com.example.mq.mqserver.mapper.MetaMapper;

import java.io.File;
import java.util.List;

/**
 * 通过这个类，整合数据库操作
 */
public class DataBaseManager {
    private MetaMapper metaMapper;
    public void init() {
        // 手动获取到metaMapper这个对象
        metaMapper = MqApplication.context.getBean(MetaMapper.class);

        if(!checkDBExist()) {
            // 数据库不存在
            // 先创建一个目录
            File dataDir = new File("./data");
            dataDir.mkdirs();
            System.out.println("[DataBaseManager] 数据库目录创建完成");
            // 1、创建数据库（只要调用了MyBatis就会自动生成数据库）
            // 2、创建表
            createTables();
            // 3、构造默认数据
            createDefaultDate();


        }else {
            // 数据库存在，什么都不做
            System.out.println("[DataBaseManager] 数据库已经存在");
        }
    }

    private void createDefaultDate() {
        // 构造一个交换机
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 创建初始化数据完成");
    }

    private void createTables() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManager] 数据表已创建完成");
    }

    private boolean checkDBExist() {
        File file = new File("./data/meta.db");
        if(file.exists()) {
            return true;
        }
        return false;
    }

    // 将其他的数据库操作也封装到这个类中
    // 插入
    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }
    public void insertQueue(MSGQueue queue) {
        metaMapper.insertQueue(queue);
    }
    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }

    // 删除
    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }
    public void deleteQueue(String queueName) {
        metaMapper.deleteQueue(queueName);
    }
    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }
    public void deleteDB() {
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if(ret) {
            System.out.println("[DataBaseManager] 数据库文件删除成功");
        } else {
            System.out.println("[DataBaseManager] 数据库文件删除失败");
        }

        File dataDir = new File("./data");
        // 使用Delete删除目录，必须确保目录里面是空的，才能删除成功
        ret = dataDir.delete();
        if(ret) {
            System.out.println("[DataBaseManager] 数据库目录删除成功");
        } else {
            System.out.println("[DataBaseManager] 数据库目录删除失败");
        }
    }

    // 查找
    public List<Exchange> selectAllExchanges() {
        return metaMapper.selectAllExchanges();
    }
    public List<MSGQueue> selectAllQueues() {
        return metaMapper.selectAllQueues();
    }
    public List<Binding> selectAllBindings() {
        return metaMapper.selectAllBindings();
    }
}
